package db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import beans.Skill;

public class SkillDAO {
	public static List getSkills() {
		try {
			Connection c = DataUtils.getDBConnection();
			
			PreparedStatement st = c.prepareStatement(SQL.map.get("SSKILLS.sql").toString());
			
			ResultSet rs = st.executeQuery();
		    
		    List skills = new ArrayList();
		    
		    while (rs.next()) {
		    
		    	
		    	skills.add(new Skill(rs.getInt("SKILL_ID"), rs.getString("DESCRIPTION")));
		    }
		    
		    rs.close();
		    st.close();
		    
		    return skills;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public static String addSkill(String description) {
		try {
			Connection c = DataUtils.getDBConnection();
			
			PreparedStatement st = c.prepareStatement(SQL.map.get("ISKILL.sql").toString());
			st.setString(1, description);
				
			st.executeUpdate();
		
			st.close();
			    
		    return "";
		} catch (SQLException s) {
			int errorCode = s.getErrorCode();
			if (errorCode == 1) {
				return "Skills must be unique.";
			} else {
				return "" + errorCode;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}


}
